package site.qjdb.service.impl;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import site.qjdb.dao.UserDao;
import site.qjdb.pojo.User;
import site.qjdb.pojo.response.CommonResult;
import site.qjdb.pojo.response.ResponseCode;
import site.qjdb.service.UserService;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;

	@Override
	public ResponseCode regist(User user) {
		// 校验学号有没有被注册过
		int i = userDao.selectUserBySno(user.getSno());
		if (i != 0) {
			return ResponseCode.ALREADY_REGIST;
		}
		userDao.addUser(user);
		return ResponseCode.SUCCESS;
	}

	@Override
	public CommonResult<User> loginBySno(User user) {
		User newUser = userDao.selectUser(user);
		// 若newUser为null则用户名或密码不正确
		if (newUser == null) {
			return new CommonResult<>(ResponseCode.INCORRECT_LOGIN);
		}
		// 登录成功
		// 更改最近一次的登录时间
		newUser.setLoginTime(new Date());
		userDao.updateUser(newUser);
		return new CommonResult<User>(ResponseCode.SUCCESS, newUser);
	}

	@Override
	public CommonResult<User> loginByCookie(User user) {
		System.out.println(123);
		User newUser = userDao.selectUser(user);
		// 若newUser为null则用户名或密码不正确
		if (newUser == null) {
			return new CommonResult<>(ResponseCode.UNREGIST);
		}
		// 登录成功
		// 修改更改最近一次的登录时间
		newUser.setLoginTime(new Date());
		userDao.updateUser(newUser);
		return new CommonResult<User>(ResponseCode.SUCCESS, newUser);
	}

	@Override
	public void modifyUserInfo(User user) {
		userDao.updateUser(user);
	}

	@Override
	public User findUser(Integer uid) {
		User user = new User();
		user.setUid(uid);
		User newUser = userDao.selectUser(user);
		return newUser;
	}

}
